package com.leyou.auth.config;

import com.leyou.auth.interceptors.LoginInterceptor;
import com.leyou.auth.utils.JwtUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author mySun
 * @create 2021-07-29 11:24
 */
public class MvcConfiguration implements WebMvcConfigurer {

    private ClientProperties clientProperties;
    private JwtUtils jwtUtils;

    public MvcConfiguration(ClientProperties clientProperties,@Lazy JwtUtils jwtUtils) {
        this.clientProperties = clientProperties;
        this.jwtUtils = jwtUtils;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册拦截器
        InterceptorRegistration interceptor = registry.addInterceptor(new LoginInterceptor(jwtUtils));
        //判断用户是否配置拦截路径，没有则使用默认配置
        if(!CollectionUtils.isEmpty(clientProperties.getIncludeFilterPaths())){
            interceptor.addPathPatterns(clientProperties.getIncludeFilterPaths());
        }
        //判断用户是否配置放行路径
        if(!CollectionUtils.isEmpty(clientProperties.getExcludeFilterPaths())){
            interceptor.excludePathPatterns(clientProperties.getExcludeFilterPaths());
        }

    }



}
